package com.lagou.controller;

import com.lagou.domain.Dept;
import com.lagou.domain.DeptMessage;
import com.lagou.domain.Emp;
import com.lagou.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dept")
public class Dept_Emp_controller {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Emp> list = deptService.findAll();
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/save")
    public String save(Emp emp) {
        deptService.save(emp);
        return "redirect:/dept/findAll";
    }

    @RequestMapping("/ajaxRequest")
    @ResponseBody
    public List<DeptMessage> ajaxRequest(@RequestBody List<DeptMessage> list) {
        System.out.println(list);
        return list;
    }

    @RequestMapping("/findById")
    public String findById(int dept_id, Model model) {
        Emp emp = deptService.findById(dept_id);
        System.out.println(emp);
        model.addAttribute("emp",emp);
        return "update";
    }


    @RequestMapping("/update")
    public String update(Emp emp){
        System.out.println(emp);
        deptService.update(emp);
        return "redirect:/dept/findAll";
    }



}
